package com.warefont.lepad.adapter;

import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.warefont.lepad.R;
import com.warefont.lepad.WXPadActivity;
import com.warefont.lepad.beans.LFDEntity;
import com.warefont.lepad.beans.LFDPoolEntity;
import com.warefont.lepad.utils.AsyncImageLoader;
import com.warefont.lepad.utils.DisplayUtil;

public class LFDPoolGridViewImageAdapter extends BaseAdapter {
	private Context context;
	private AsyncImageLoader asyncImageLoader = null;
	private List<LFDEntity> lfdEntityList = null;
	private static final int DATE_CHANGE = 0;
	private WeakReference<Activity> weak;

	private Integer[] imageIds = { R.drawable.a0, R.drawable.a1, R.drawable.a2,
			R.drawable.a3, R.drawable.a4, R.drawable.a5, R.drawable.a6,
			R.drawable.a7, R.drawable.a8, R.drawable.a9, R.drawable.a10,
			R.drawable.a11 };
	private Integer[] imageIdsForOne = { R.drawable.a1_1, R.drawable.a1_2,
			R.drawable.a1_3, R.drawable.a1_4 };
	private Integer[] imageIdsForEight = { R.drawable.a8_1, R.drawable.a8_2,
			R.drawable.a8_3, R.drawable.a8_4 };

	public LFDPoolGridViewImageAdapter(Context context) {
		this.context = context;
		this.weak = new WeakReference<Activity>((Activity) context);
	}

	@Override
	public int getCount() {
		if (lfdEntityList != null && asyncImageLoader != null
				&& !lfdEntityList.isEmpty()) {
			return lfdEntityList.size();
		}
		// return imageIds.length;
		return 8;
	}

	@Override
	public Object getItem(int position) {
		if (lfdEntityList != null) {
			return lfdEntityList.get(position);
		}
		return imageIds[position];
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	/**
 * 
 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		int resourceId = 0;
		ImageView imageView = new ImageView(context);
		if (lfdEntityList != null && !lfdEntityList.isEmpty()) {
			LFDEntity lfdEntity = lfdEntityList.get(position);
			String imageName = lfdEntity.getOverviewImg();

			if (imageName.contains("_")) {
				String[] temp = imageName.split("_");
				int a = Integer.parseInt(temp[0]);
				int b = Integer.parseInt(temp[1]);

				switch (a) {
				case 1:
					resourceId = imageIdsForOne[b - 1];
					break;
				case 8:
					resourceId = imageIdsForEight[b - 1];
					break;
				}
			} else {
				int imageId = Integer.parseInt(imageName);
				resourceId = imageIds[imageId];

			}
			
			imageView.setImageBitmap(DisplayUtil.decodeSampledBitmapFromResource(context.getResources(), resourceId, 190, 155));
			// imageView.setImageResource(resourceId);
			imageView.setBackgroundResource(R.drawable.image_background);
			// 设置ImageView的伸缩规格，用了自带的属性值
			imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
			// 设置布局参数
			imageView.setLayoutParams(new GridView.LayoutParams(190, 155));

			return imageView;
		} else {

			imageView.setImageBitmap(DisplayUtil.decodeSampledBitmapFromResource(context.getResources(), R.drawable.image_loading, 190, 155));
			
			imageView.setBackgroundResource(R.drawable.image_background);
			// 设置ImageView的伸缩规格，用了自带的属性值
			imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
			// 设置布局参数
			imageView.setLayoutParams(new GridView.LayoutParams(190, 155));

			return imageView;
		}

	}

	/**
	 * 
	 * @param lfdEntityList
	 * @param asyncImageLoader
	 */
	public void setAsyncImageLoaderAndLFDEntityList(
			List<LFDEntity> lfdEntityList, AsyncImageLoader asyncImageLoader) {
		this.asyncImageLoader = asyncImageLoader;
		this.lfdEntityList = lfdEntityList;
	}

	/**
	 * 
	 */
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case DATE_CHANGE:
				final WXPadActivity activity = (WXPadActivity) weak.get();

				DisplayUtil.initGridViewAndLinearLayoutForGridView(
						activity.getLfdPoolGridView(),
						activity.getLfdPoolGridViewImageAdapter(),
						activity.getLinearLayoutForPoolGridView(),
						WXPadActivity.lfdPoolGridViewRowNum, 190, context);
				notifyDataSetChanged();
			}
			// msg.recycle();
		}

	};

}
